package com.iqiyi.pps.epg.core.rpc.reservation;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.reservation.TReservation;
import com.iqiyi.pps.epg.api.model.web.reservation.TReservationPage;
import com.iqiyi.pps.epg.api.server.web.reservation.ReservationWebService;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.model.reservation.ReserveControl;
import com.iqiyi.pps.epg.core.service.reservation.ReservationService;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReservationWebServiceImpl implements ReservationWebService.Iface {
    @Override
    public TReservation getById(long id) throws TException {
        ReservationService service = (ReservationService) ApplicationContextUtil.getBean(ReservationService.class);
        ReserveControl entity = service.getById(id);
        if (entity == null) {
            return null;
        }
        return entity.entityToThrift();
    }

    @Override
    public boolean remove(long id) throws TException {
        ReservationService service = (ReservationService) ApplicationContextUtil.getBean(ReservationService.class);
        return service.del(id);
    }

    @Override
    public int add(List<TReservation> entities) throws TException {
        ReservationService service = (ReservationService) ApplicationContextUtil.getBean(ReservationService.class);
        return service.add(entities);
    }

    @Override
    public boolean save(TReservation entity) throws TException {
        ReservationService service = (ReservationService) ApplicationContextUtil.getBean(ReservationService.class);
        return service.save(ReserveControl.thriftToEntity(entity));
    }

    @Override
    public TReservationPage get(TPage tpage, TFilter tfilter) throws TException {
        List<PropertyFilter> filters = buildFilters(tfilter);
        Page<ReserveControl> page = buildPage(tpage);
        ReservationService service = (ReservationService) ApplicationContextUtil.getBean(ReservationService.class);
        page = service.get(page, filters);
        if (page != null) {
            return toThriftPage(page);
        }
        return null;
    }

    public List<PropertyFilter> buildFilters(TFilter filter) {
        List<PropertyFilter> filterList = new ArrayList();
        if (filter.getFiltersSize() > 0) {
            Map<String, String> filterMap = filter.getFilters();
            for (Map.Entry<String, String> item : filterMap.entrySet()) {
                String name = item.getKey();
                String value = item.getValue();
                if (StringUtils.isNotBlank(value)) {
                    PropertyFilter temp = new PropertyFilter(name, value);
                    filterList.add(temp);
                }
            }
        }
        return filterList;
    }

    public Page buildPage(TPage tPage) {
        if (tPage == null) {
            return new Page(20);
        } else {
            Page page = new Page();
            page.setAutoCount(tPage.isAutoCount());
            if (tPage.getPageNo() > 0) {
                page.setPageNo(tPage.getPageNo());
            } else {
                page.setPageNo(1);
            }
            if (tPage.getPageSize() > 0) {
                page.setPageSize(tPage.getPageSize());
            } else {
                page.setPageSize(20);
            }
            page.setOrder(tPage.getOrder());
            page.setOrderBy(tPage.getOrderBy());
            return page;
        }
    }

    public TReservationPage toThriftPage(Page<ReserveControl> page) {
        TReservationPage tReservationPage = new TReservationPage();
        TPage tPage = new TPage();
        tPage.setOrderBy(page.getOrderBy());
        tPage.setOrder(page.getOrder());
        tPage.setPageNo(page.getPageNo());
        tPage.setPageSize(page.getPageSize());
        tPage.setAutoCount(page.isAutoCount());
        tPage.setTotalCount(page.getTotalCount());
        tReservationPage.setPage(tPage);

        // 处理查询结果
        List<TReservation> tReservations = new ArrayList();
        if (CollectionUtils.isNotEmpty(page.getResult())) {
            for (ReserveControl item : page.getResult()) {
                tReservations.add(item.entityToThrift());
            }
        }
        tReservationPage.setData(tReservations);
        return tReservationPage;
    }
}
